import { apiClient } from "./client";
import type { CardSegment, DebitAccount, CardTag } from "../types/api";
import type {
    CardProduct,
    CreateCardRequest,
    CreateCardResponse,
    CreateCardHolderRequest,
    CreateCardHolderResponse,
    CardListParams,
    Card,
    CardSensitiveDetails,
    CardTransactionQueryParams,
    CardTransactionResponse,
    CardBalanceLogParams,
    CardTransferLogParams,
    CardBalanceLogItem,
    CardHolder,
    CardSetPinRequest,
    CardActivateRequest,
    SendActivationCodeRequest,
    TransferInfoRequest,
} from "../types/card";
import type { CardWithdrawParams, CardUpdateRequest } from "../types/withdraw";

const API_ENDPOINTS = {
    // Get card segments
    getSegments: async (): Promise<CardSegment[]> => {
        return apiClient.get("/api/v1/card/segments");
    },

    // Get debit accounts
    getDebitAccounts: async (): Promise<DebitAccount[]> => {
        return apiClient.get("/api/v1/card/debit-accounts");
    },

    // Get card tags
    getTags: async (): Promise<CardTag[]> => {
        return apiClient.get("/api/v1/card/tags");
    },

    // Get card product list
    getCardProductList: async (): Promise<CardProduct[]> => {
        return apiClient.get("/api/v1/card/card-product-list");
    },

    // Get card holder select options
    getCardHolderSelect: async (): Promise<CardHolder[]> => {
        return apiClient.get("/api/v1/card/cardholder-select");
    },

    // Get card list
    getCardList: async (params?: CardListParams): Promise<Card[]> => {
        return apiClient.get("/api/v1/card/card-list", { params });
    },

    // Create card
    createCard: async (data: CreateCardRequest): Promise<CreateCardResponse> => {
        return apiClient.post("/api/v1/card/create-card", data);
    },

    // Create card holder
    createCardHolder: async (
        data: CreateCardHolderRequest
    ): Promise<CreateCardHolderResponse> => {
        return apiClient.post("/api/v1/card/create-cardholder", data);
    },

    // Batch open cards
    batchOpenCards: async (data: any): Promise<void> => {
        return apiClient.post("/api/v1/card/batch-open", data);
    },

    // Single card opening
    openCard: async (data: any): Promise<void> => {
        return apiClient.post("/api/v1/card/open", data);
    },

    // Cancel card
    cancelCard: async (data: {
        cardId: number;
        verifyCode: string;
    }): Promise<void> => {
        return apiClient.post("/api/v1/card/card-cancelled", data);
    },

    // Recharge card
    rechargeCard: async (data: {
        amount: number;
        cardId: number;
        walletType: number;
    }): Promise<void> => {
        return apiClient.post("/api/v1/card/card-recharge", data);
    },

    // Withdraw from card
    withdrawCard: async (data: {
        Amount: number;
        VerifyCode: string;
        WithdrawUsdtAddress: string;
    }): Promise<void> => {
        return apiClient.post("/api/v1/amount/withdraw", data);
    },

    // Submit card withdraw request
    submitCardWithdraw: async (data: CardWithdrawParams): Promise<void> => {
        return apiClient.post("/api/v1/card/card-withdraw", data);
    },

    // Get card sensitive details
    getCardSensitiveDetails: async (data: {
        cardId: number;
        verifyCode: string;
    }): Promise<CardSensitiveDetails> => {
        return apiClient.get("/api/v1/card/card-sensitive-details", {
            params: data,
        });
    },

    // 设置卡片PIN码
    setCardPin: async (params: CardSetPinRequest): Promise<any> => {
        return apiClient.post("/api/v1/card/set-pin", params);
    },

    // 激活卡片
    activateCard: async (params: CardActivateRequest): Promise<any> => {
        return apiClient.post("/api/v1/card/activate", params);
    },

    // 刷新卡余额
    refreshCardBalance: async (cardId: number): Promise<string> => {
        const response = await apiClient.get(`/api/v1/card/refresh-balance?CardId=${cardId}`);
        return response;
    },

    // Get card transactions
    getCardTransactions: async (
        params: CardTransactionQueryParams
    ): Promise<CardTransactionResponse> => {
        return apiClient.get("/api/v1/card/card-transactions", { params });
    },

    // Get card balance logs
    getCardBalanceLogList: (
        params: CardBalanceLogParams
    ): Promise<{ list: CardBalanceLogItem[]; total: number }> => {
        return apiClient.get("/api/v1/card/card-balance-log-list", { params });
    },
    // Get card balance logs
    getCardTransferLogList: (
        params: CardTransferLogParams
    ): Promise<{ list: []; total: number }> => {
        return apiClient.get("/api/v1/card/transfer-list", { params });
    },
    // Get card holder list
    getCardHolderList: async (
        params: { page: number; pageSize: number }
    ): Promise<{ list: CardHolder[]; total: number }> => {
        const response = await apiClient.get("/api/v1/card/cardholder-list", {
            params,
        });
        return response;
    },

    // Update card
    updateCard: async (params: CardUpdateRequest): Promise<void> => {
        return apiClient.post('/api/v1/card/card-update', params);
    },
    // 重新发送激活码
    resendActivationCode: async (params: SendActivationCodeRequest) => {
        return apiClient.get('/api/v1/card/get-active-code', { params })
    },
    // 查询卡片转账信息
    queryTransferInfo: async (params?: TransferInfoRequest) => {
        return apiClient.get("/api/v1/card/transfer-config", { params });
    },
    // 通过卡号查询收款人邮箱
    queryEmailByCardNo: async (params: any) => {
        return apiClient.get("/api/v1/card/search-card-info", { params });
    },
    // 提交转账
    sumbitTransfer: async (data: {
        CardNumber: string,
        Amount: number
    }): Promise<void> => {
        return apiClient.post("/api/v1/card/transfer", data);
    },
    // 获取卡配置信息
    getCardConfig: async () => {
        return apiClient.get("/api/v1/card/virtual-create-card-config");
    },
};

export const cardApi = API_ENDPOINTS;
